<?php
namespace Ygcl\Controller;
use Admin\Controller\AdminbaseController;
class LessonadminController extends AdminbaseController {
	
	protected $m;
	protected $districtService;
	protected $lessonService;
	protected $classtypeService;
	protected $order;
	protected $mclass;
	
	function _initialize() {
		parent::_initialize();
		$this->m = D('YgclLesson');
		$this->districtService = D('District','Service');
		$this->order = D('YgclOrder');
		$this->mclass = D('YgclClass');
		$this->classtypeService = D('ClassType','Service');
		$this->lessonService = D('Lesson','Service');
	}
	
    public function index(){
    	$this->districtoptions = $this->districtService->_get_select_options($_GET['did']);
    	
    	$this->display();
    }
    
    public function edit(){
    	$id = I('get.id');
    	if($id){
    		$map['id'] = $id;
    		$this->info = $this->m->where($map)->find();
    	}
    	
    	$this->districtoptions = $this->districtService->_get_select_options($this->info['did']);
    	$this->display();
    }
    
    public function edit_post(){
    	if(IS_POST){
    		$_POST['update_time'] = Date('Y-m-d H:i:s');
    		if($_POST['id']){
    			if($this->m->create()){
    				if($this->m->save()){
    					$this->success('更新成功');
    				}else{
    					$this->error('更新失败');
    				}
    			}
    		}else{
    			$_POST['create_time'] = $_POST['update_time'];
    			if($this->m->create()){
    				$id = $this->m->add();
    				if($id){
    					$this->success('添加成功');
    				}else{
    					$this->error('添加失败');
    				}
    			}
    		}
    	}
    }
    
    public function del(){
    	$id = array_unique((array)I('ids',0));
    	if ( empty($id) ) {
    		$this->error('请选择要操作的数据!');
    	}
    	$map = array('id' => array('in', $id));
    	$info = $this->m->where($map)->find();
    	if($this->m->where($map)->delete()){
    		$this->success('删除成功',U('index'));
    	} else {
    		$this->error('删除失败！');
    	}
    }
    
    public function changestatus(){
    	$data[$this->m->getPk()] = $_REQUEST['id'];
    	$data['status'] = $_REQUEST['status'];
    	$data['update_time'] = date('Y-m-d H:i:s');
    	if($this->m->save($data)){
    		$this->lessonService->_create_cache_lesson();
    		$this->success('状态更新成功');
    	}else{
    		$this->error('状态更新失败');
    	}
    }
    
    public function lists(){
    	$this->display();
    }
    
    public function clist(){
    	$this->cid = I('get.cid');
    	$this->classinfo = D('YgclClass')->where('id = '.$this->cid)->find();
    	$this->display();
    }
    
    public function editlesson(){
    	
    	$id = I('get.id');
    	if($id){
    		$map['id'] = $id;
    		$this->info = $this->m->where($map)->find();
    	}
    	
    	if($this->info){
    		$cid = $this->info['cid'];
    	}else{
    		$cid = I('get.cid');
    	}
    	$classinfo = $this->mclass->where('id = '.$cid)->find();//课程信息
    	$did = $classinfo['did'];
    	
    	$this->district = D("YgclDistrict")->where('id = '.$did)->getField('title');
    	$this->class = $classinfo['title'];
    	$this->classtypeoptions = $this->classtypeService->_get_select_options($cid,$this->info['lessontype']);
    	$this->cid = $cid;
    	$this->display();
    }
    
    public function editlesson_post(){
    	if(IS_POST){
    		if(!$_POST['lessontype']){
    			$this->error("分类不能为空，请先添加分类");
    			exit();
    		}
    		$_POST['update_time'] = Date('Y-m-d H:i:s');
    		if($_POST['id']){
    			if($this->m->create()){
    				if($this->m->save()){
    					$this->lessonService->_create_cache_lesson($_POST['cid']);
    					$this->success('更新成功');
    				}else{
    					$this->error('更新失败');
    				}
    			}
    		}else{
    			$_POST['create_time'] = $_POST['update_time'];
    			if($this->m->create()){
    				$id = $this->m->add();
    				if($id){
    					$this->lessonService->_create_cache_lesson($_POST['cid']);
    					$this->success('添加成功');
    				}else{
    					$this->error('添加失败');
    				}
    			}
    		}
    	}
    }
    
    public function dellesson(){
    	$id = array_unique((array)I('ids',0));
    	if ( empty($id) ) {
    		$this->error('请选择要操作的数据!');
    	}
    	$map = array('id' => array('in', $id));
    	$info = $this->m->where($map)->find();
    	if($this->m->where($map)->delete()){
    		$this->lessonService->_create_cache_lesson($info['cid']);
    		$this->success('删除成功',U('clist',array(cid=>$info['cid'])));
    	} else {
    		$this->error('删除失败！');
    	}
    }
    
    public function changelessonstatus(){
    	$data[$this->m->getPk()] = $_REQUEST['id'];
    	$data['status'] = $_REQUEST['status'];
    	$data['update_time'] = date('Y-m-d H:i:s');
    	$cid = $this->m->where(array('id'=>$_REQUEST['id']))->getField('cid');
    	if($this->m->save($data)){
    		$this->lessonService->_create_cache_lesson($cid);
    		$this->success('状态更新成功');
    	}else{
    		$this->error('状态更新失败');
    	}
    }
    
    public function studentlist(){
    	$xnid = I('xnid');
    	$xn_now = R('Api/Termtime/get_term_time_now');
    	if(!$xnid) $xnid = $xn_now['id'];
    	
    	$this->all = R('Api/Termtime/get_term_time_all');
    	//$this->xuenian = R('Api/Termtime/get_term_time_now');
    	$this->xnid = $xnid;
    	$this->display();
    }
    
    public function detail(){
    	$id = I('id');
    	$info = D('LessonStudentRelation')->where(array('id'=>$id))->relation(true)->find();
    	$this->info = $info;
    	$this->display();
    }
    
    public function detail_post(){
    	if(IS_POST){
    		$m = M('LessonStudentRelation');
   			$_POST['update_time'] = Date('Y-m-d H:i:s');
   			if($m->create()){
   				if($m->save()){
    				$this->success('更新成功');
    			}else{
    				$this->error('更新失败');
    			}
   			}
   		}
    }
    
    public function get_xuenian($xnid){
    	$info = M('YgclTermTime')->where(array('id'=>$xnid))->find();
    	return $info['title'].'('.$info['start_time'].'到'.$info['end_time'].')';
    }
    
    public function changeclass(){
    	$id = I('id');
    	$lessonstudent = D('LessonStudentRelation')->where(array('id'=>$id))->relation(true)->find();
		
    	$did = R('Apiadmin/getxiaoqurole');
    	if($did){
    		$map['id'] = $did;
    	}
    	$map['status'] = 1;
    	$this->xiaoqu = D('YgclDistrict')->where($map)->order('listorder desc')->select();
		$this->lsinfo = $lessonstudent;
		$this->xueqi = R('Api/Term/get_term_infos_wgq');
    	$this->display();
    }
    
    public function changeclass_post(){
    	if(IS_POST){
    		$tid = I('tid');
    		$lid = I('lid');
    		$lsid = I('lsid');
    		
    		$lsm = D('LessonStudentRelation');
    		$lsinfo = $lsm->where(array('id'=>$lsid))->find();
    		if($tid == $lsinfo['tid'] && $lid == $lsinfo['lid']){
    			$this->error('同一学期，同一班级，不能换班');
    			exit();
    		}
    		
    		//判断学年和班级人数是否满额
    		$terminfo = R('Api/Term/get_term_by_id',array('id'=>$tid));
    		$xuenian = $terminfo['studyyear'];
    		//$data = R('Api/Lessonapi/getnumber_by_xuenian',array('xuenian'=>$xuenian,'lid'=>$lid));
    		//$status = array('status'=>1,'info'=>'');
    		//if(!$data['bool']){
    		//	$xnid = $data['xnid'];
    		//	$info = M('YgclTermTime')->where(array('id'=>$xnid))->find();
    		//	$html = '此班级('.$info['start_time'].'到'.$info['end_time'].')人数已报满,请选择其他班级或有特殊情况请联系校方递交申请';
    		//	$status = array('status'=>0,'info'=>$html);
    		//}
    		//if($status['status']){
    			
    			$data = array(
    					'id' => $lsid,
    					'tid' => $tid,
    					'xuenian' => $xuenian,
    					'end_time' => $terminfo['end_time'],
    					'did' => I('did'),
    					'cid' => I('cid'),
    					'lid' => I('lid'),
    					'update_time' => Date('Y-m-d H:i:s')
    			);
    			$lsm->create($data);
    			if($lsm->save()){
    				$data2 = array(
    						'lsid' => $lsid,
    						'original_tid' => $lsinfo['tid'],
    						'next_tid' => $tid,
    						'original_lid' => $lsinfo['lid'],
    						'next_lid' => $lid,
    						'remark' => $_POST['remark'],
    						'create_time' => Date('Y-m-d H:i:s')
    				);
    				$srm = M('LessonRecord');
    				$srm->add($data2);
    			
    				//修改班级人数
    				R('Api/Lessonapi/changelsinfo',array('old_tid'=>$lsinfo['tid'],'tid'=>$tid,'old_lid'=>$lsinfo['lid'],'lid'=>$lid));
    			}
    			$this->success('换班成功');
    		//}else{
    		//	$this->error('超过班级人数，换班失败');
    		//}
    	}
    }
    
    
    public function studentdel(){
    	$m = M('LessonStudentRelation');
    	$id = array_unique((array)I('ids',0));
    	if ( empty($id) ) {
    		$this->error('请选择要操作的数据!');
    	}
    	$map = array('id' => array('in', $id));
    	$info = $m->where($map)->find();
    	if($m->where($map)->delete()){
    		R('Api/Lessonapi/lesson_number_dec',array('tid'=>$info['tid'],'lid'=>$info['lid']));
    		$this->success('删除成功');
    	} else {
    		$this->error('删除失败！');
    	}
    }
}